#!/usr/bin/env bash

# @see https://apple.stackexchange.com/questions/10467/how-to-increase-keyboard-key-repeat-rate-on-os-x
defaults write NSGlobalDomain KeyRepeat -int 2
defaults write NSGlobalDomain InitialKeyRepeat -int 10
# thin font
# defaults write -g AppleFontSmoothing -int 0

dotfiles_dir="$HOME/.dotfiles"
home_config_dir="$HOME/.config"

echo "link config file"
target_folders=("tmux" "kitty")
for folder_name in "${target_folders[@]}"; do
  folder="$dotfiles_dir/config/$folder_name"
  target_folder="$home_config_dir/$folder_name"

  if [ -d "$target_folder" ]; then
    rm -rf "$target_folder"
    echo "Removed existing $target_folder"
  fi

  ln -s "$folder" "$target_folder"
  echo "Created symlink from $folder to $target_folder"
done

# ###########################################################
# Install non-brew various tools (PRE-BREW Installs)
# ###########################################################
echo "ensuring build/install tools are available"
if ! xcode-select --print-path &> /dev/null; then
  # Prompt user to install the XCode Command Line Tools
  xcode-select --install &> /dev/null
  # - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  # Wait until the XCode Command Line Tools are installed
  until xcode-select --print-path &> /dev/null; do
    sleep 5
  done
  print_result $? ' XCode Command Line Tools Installed'
  # Prompt user to agree to the terms of the Xcode license
  # https://github.com/alrra/dotfiles/issues/10
  sudo xcodebuild -license
  print_result $? 'Agree with the XCode Command Line Tools licence'
fi

# ###########################################################
# install homebrew (CLI Packages)
# ###########################################################
brew_bin=$(which brew) 2>&1 > /dev/null
if [[ $? != 0 ]]; then
  echo "installing homebrew"
  /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
  if [[ $? != 0 ]]; then
    echo "unable to install homebrew, script $0 abort!"
    exit 2
  fi
fi

###########################################################
# Config
###########################################################
shopt -s dotglob
for file in "$HOME/.dotfiles/link"/*; do
  if [ -f "$file" ]; then
    filename=$(basename "$file")
    ln -s "$file" "$HOME/$filename"
    echo "Created symlink: $HOME/$filename -> $file"
  fi
done
shopt -u dotglob

# Change to zsh
chsh -s $(which zsh)
reload
proxy

# Function to install a package with a given package manager
install_package() {
  local package_manager="$1"
  local package="$2"
  case "$package_manager" in
    "brew")
      if brew list "$package" &> /dev/null; then
        echo "Package '$package' is already installed."
      else
        brew install "$package"
        if [ $? -eq 0 ]; then
          echo "Successfully installed '$package'."
        else
          echo "Error installing '$package'."
          exit 1
        fi
      fi
      ;;

    "npm")
      if npm list -g "$package" &> /dev/null; then
        echo "Package '$package' is already installed globally."
      else
        npm install -g "$package"
        if [ $? -eq 0 ]; then
          echo "Successfully installed '$package' globally."
        else
          echo "Error installing '$package' globally."
          exit 1
        fi
      fi
      ;;
      
    *)
      echo "Unsupported package manager: $package_manager"
      exit 1
      ;;
  esac
}

echo "install packages"
brew tap v2raya/v2raya
brew_packages=(
  "v2raya"
  "kitty"
  "licecap"
  "gopls"
  "rust-analyzer"
  "lua-language-server"
  "clang-format"
  "stylua"
  "helix"
  "zig"
  "golang"
  "rustup-init"
  "node"
  "yarn"
  "lua@5.1"
  "luarocks"
  "ninja"
  "zls"
  "llvm"
  "ripgrep"
  "bat"
  "make"
  "cmake"
  "tmux"
  "fzf"
  "docker"
  "skim"
  "qq"
  "wechat"
  "obs"
  "binutils"
)

# Install Homebrew packages
for package in "${brew_packages[@]}"; do
  install_package "brew" "$package"
done

brew service start v2raya
brew install neovim --HEAD

# Config git
git config --global user.name "glepnir"
git config --global user.email "glephunter@gmail.com"
git config --global core.editor "nvim"
git config --global credential.helper store
git config --global core.excludesFile $HOME/.gitignore
git config --global init.defaultBranch 'main'

# My neovim config
echo "Download neovim config"
git clone https://github.com/glepnir/nvim ~/.config/nvim
mkdir "$HOME/.local/share/nvim"
mkdir "$HOME/.local/share/nvim/bin"
ln -s "$dotfiles/dbgvim" "$HOME/.local/share/nvim/bin/dbgvim"

/usr/local/opt/fzf/install
luarocks --lua-version=5.1 install vusted

echo $CARGO_HOME $RUSTUP_HOME
rustup-init
rustup default stable

# tmux plugin management
git clone https://github.com/tmux-plugins/tpm ~/.local/share/tmux/tpm

echo "Config font"
# Set source and target directories
fonts_dir=$( cd "$( dirname "$0" )" && pwd )
find_command="find \"$fonts_dir\" \( -name '*.[o,t]tf' -or -name '*.pcf.gz' \) -type f -print0"

# Copy all fonts to user fonts directory
brew tap homebrew/cask-fonts
brew install font-symbols-only-nerd-font
brew install font-Iosevka

# npm relate
npm_packages=(
  "eslint"
  "typescript"
  "typescript-language-server"
  "prettier"
  "vscode-langservers-extracted"
  "bash-language-server"
  "vite"
)
# Install npm packages globally
for package in "${npm_packages[@]}"; do
  install_package "npm" "$package"
done

brew cleanup
echo "All done"
